package javamusic.Servlet.Reserva;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import javamusic.excepcion.DAOExcepcion;
import javamusic.modelo.Reserva;
import javamusic.negocio.GestionReserva;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletProductoBuscar
 */
@WebServlet("/ServletReservaBuscar")
public class ServletReservaBuscar extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ServletReservaBuscar() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String  dni = request.getParameter("dni");	
		String  fecInicio = request.getParameter("fecInicio");
		String  fecFin = request.getParameter("fecFin");
		
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy",new Locale("es","ES"));
		
		Integer intDNI = 0;
		
		if(dni!= null && dni != "") intDNI = Integer.parseInt(dni);
		
		GestionReserva negocio = new GestionReserva();  
		
		try {
			
			if(fecInicio == null || fecInicio == "") fecInicio= "01/01/1901";
			if(fecFin == null || fecFin == "") fecFin= "01/01/1901";
			
			Date fechaInicio = format.parse(fecInicio);
			Date fechaFin = format.parse(fecFin);
			Collection<Reserva> lstReserva = negocio.BuscarReserva(intDNI, new java.sql.Date(fechaInicio.getTime()), new java.sql.Date(fechaFin.getTime()));	
			request.setAttribute("Busqueda_Result", lstReserva);
			RequestDispatcher rd = request.getRequestDispatcher("frmBusquedaReserva.jsp");		
			rd.forward(request, response);
		} catch (DAOExcepcion | ParseException e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}
		

}
